package com.qf.emp.controller;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "CreateCodeController", value = "/createCode")
public class CreateCodeController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//    1。 创建验证码图片
        ValidateCode validateCode = new ValidateCode(200, 30, 4, 20);
//        1.1 获取生成验证码的文字
        String codes = validateCode.getCode();
//        1.2 创建 session 对象
        HttpSession session = request.getSession();
//        1.3 将生成的验证码文字存储到 session 对象中
        session.setAttribute("codes",codes);

//    2.将验证码图片，响应给客户端
        validateCode.write(response.getOutputStream());


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
